// show2003.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;

#include <boost/filesystem.hpp>
using namespace boost;
using namespace boost::filesystem;

const int MAX_DBFFILE_LEN = 1*1024*1024;

void GetAllFilePath(string directory_path, vector<string>& arrFilePath)
{
  path dir_path(directory_path);

  dir_path.string();

  directory_iterator end;

  for(directory_iterator pos(dir_path); pos != end; ++pos)
  {
    path tmp = *pos;

    if(is_directory(tmp))
    {
      GetAllFilePath( tmp.string(), arrFilePath );
    }
    else
    {
      arrFilePath.push_back(tmp.string());
    }
  }
}

void ReadToBuffer(string& buffer, string file_path)
{
  FILE* pFile = fopen(file_path.c_str(), "rb");
  char* pszFileBuffer = new char[MAX_DBFFILE_LEN];
  int nBufferSize = fread(pszFileBuffer, sizeof(char), MAX_DBFFILE_LEN, pFile);
  buffer.assign(pszFileBuffer, nBufferSize);
  delete[] pszFileBuffer;
  fclose(pFile);
}

int _tmain(int argc, _TCHAR* argv[])
{
  vector<string> arrBuffer;
  vector<string> arrFilePath;

  GetAllFilePath("D:\\outtest\\show2003_2\\file_source", arrFilePath);

  vector<string>::size_type i = 0;
  vector<string>::size_type size = arrFilePath.size();
  for(i = 0; i < size; ++i)
  {
    string buffer;
    ReadToBuffer(buffer, arrFilePath[i]);
    arrBuffer.push_back(buffer);
  }

  i = 0;
  size = arrBuffer.size();
  while(1)
  {
    vector<string>::size_type index = (i++)%size;
    FILE* pFile = fopen("D:\\outtest\\show2003\\source\\show2003.dbf", "wb");
    const string& buffer = arrBuffer[index];
    fwrite(buffer.c_str(), sizeof(char), buffer.length(), pFile);
    fclose(pFile);

    cout << arrFilePath[index] << " ############### modify!!!" << endl << endl;

    Sleep(800);
  }
  
	return 0;
}